package com.bdqn.petmalldemo.common;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;

/**
 * @Author:彭俊
 * @description:返回结果实体类
 * @date:2020/2/121715
 */

@ApiModel
@Data
public class ReturnResult implements Serializable{
    private static final long serialVersionUID=-7016961617960030273L;
    @ApiModelProperty(value = "响应状态码")
    private  Integer code;
    @ApiModelProperty(value = "响应消息")
    private  String msg;
    @ApiModelProperty(value = "用户登录后的token")
    private  String  token;
    @ApiModelProperty(value = "响应数据")
    private Object data;

    private ReturnResult(){

    }
    /**
     * 当后台操作成功时响应到前台
     */
    public static ReturnResult ok(){
        ReturnResult returnResult=new ReturnResult();
        returnResult.setCode(ResultCodeEnum.SUCCESS.getCode());
        returnResult.setMsg(ResultCodeEnum.SUCCESS.getMsg());
        return  returnResult;
    }

    /**
     * 后台异常响应前台
     * @return
     */
        public  static ReturnResult error(){
            ReturnResult returnResult=new ReturnResult();
            returnResult.setCode(ResultCodeEnum.FAILED.getCode());
            returnResult.setMsg(ResultCodeEnum.FAILED.getMsg());
            return  returnResult;
        }

    /**
     * 自定义信息
     * @param msg
     * @return
     */
    public ReturnResult msg(String msg){
            this.msg=msg;
            return  this;
        }

    /**
     * 自定义编码
     * @param code
     * @return
     */
    public ReturnResult code(Integer code){
            this.code=code;
            return  this;
        }
    public ReturnResult token(String token){
        this.token=token;
        return this;
    }
    public ReturnResult data(Object object){
        this.data=object;
        return  this;
    }

    @Override
    public String toString() {
      return JSONObject.toJSONString(this);
    }
}
